const axios = require('axios')

exports.getBalance = async (address, page, size) => {
    address = address || '0x23f4569002a5A07f0Ecf688142eEB6bcD883eeF8'
    page = page || 0
    size = size || 20
    // const API_KEY = 'VKMW99Y8ME3ACDZE6WQGAB2JWNZQMUGTB2'
    // const url = `https://api.etherscan.io/api
    // ?module=account
    // &action=balance
    // &address=${address}
    // &tag=latest
    // &apikey=${API_KEY}`;
    const url = `https://api.blockchain.info/v2/eth/data/account/${address}/wallet?page=${page}&size=${size}`;

    const response = await axios.get(url);
    return response.data
}

// exports.getBalance = async (req, res) => {
//     const address = '0x23f4569002a5A07f0Ecf688142eEB6bcD883eeF8';
//     const API_KEY ='VKMW99Y8ME3ACDZE6WQGAB2JWNZQMUGTB2';
  
//     if (!address) {
//       return res.status(400).json({ error: '地址不能为空' });
//     }
  
//     try {
//       const url = `https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=${API_KEY}`;
//       console.log(url);
      
//       const response = await axios.get(url, {
//         timeout: 10000,
//         maxRedirects: 5,
//       });
  
//       res.json({
//         address,
//         balance_wei: response.data.result,
//         balance_eth: (Number(response.data.result) / 1e18).toString(),
//       });
//     } catch (error) {
//       console.error("请求错误：", error.message);
//       if (error.response) {
//         console.log(error.response.status);
//         console.log(error.message);
        
//         // return res.status(error.response.status).json({ error: error.message });
//       }
//       console.log('请求失败，可能是网络或API KEY问题。');
      
//     //   res.status(500).json({ error: '请求失败，可能是网络或API KEY问题。' });
//     }
//   };